/**
 * 
 */
package com.antilia.infomaresme.provider;

import java.util.Iterator;

import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;

import com.antilia.infomaresme.InfomaresmeApplication;
import com.antilia.infomaresme.data.dao.interfaces.IUserMessagesDao;
import com.antilia.infomaresme.data.dataobjects.User;
import com.antilia.infomaresme.data.dataobjects.UserMessage;

/**
 * @author Ernesto
 *
 */
public abstract class MessagesDataProvider extends SortableDataProvider<UserMessage>  implements ISortableDataProvider<UserMessage> {

	private static final long serialVersionUID = 1L;
		
	/**
	 * 
	 */
	public MessagesDataProvider() {
		
	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.model.IDetachable#detach()
	 */
	public void detach() {

	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#iterator(int, int)
	 */
	public Iterator<? extends UserMessage> iterator(int first, int count) {
		boolean isAscending = (getSort() != null && getSort().getProperty()==null) || (getSort() != null && getSort().isAscending());
		return getIUserMessagesDao().getItems(getUser(), first, count, isAscending).iterator();
	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#size()
	 */
	public int size() {
		return getIUserMessagesDao().countItems(getUser()).intValue();
	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#model(java.lang.Object)
	 */
	public IModel<UserMessage> model(UserMessage object) {
		return new MessagesLdm(object);
	}

	protected final IUserMessagesDao getIUserMessagesDao() {
		return InfomaresmeApplication.getQAApplication().getUserMessagesDao();
	}
	
	public abstract User getUser();
}
